package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.User;
import com.tongzhan.entity.religion.UsualTask;
import com.tongzhan.service.UserService;
import com.tongzhan.service.UsualService;
import com.tongzhan.service.UsualTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/usual")
public class UsualController {

    @Resource
    private UserService userService;

    @Resource
    private UsualService usualService;

    @Resource
    private UsualTaskService usualTaskService;


    /**
     * 新增平安报送任务接口
     *
     * @param userId 用户id
     * @param title 主题
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return Return
     */
    @PostMapping("/newUsualTask")
    public Return newUsualTask(String userId, String title, String startTime, String endTime) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(title) || "".equals(title.trim())
                || StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (title.trim().length() > 500) {
            return new Return(Return.FAIL, "标题过长");
        }
        UsualTask task = new UsualTask();
        //用户信息
        User user = userService.selectByUserId(userId);
        if (user == null) {
            return new Return(Return.FAIL, "用户不存在");
        }
        //报送任务编码：用户名+时间
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String usualCode = user.getUserName() + df.format(LocalDateTime.now());
        task.setUsualCode(usualCode);
        task.setTitle(title.trim());
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate startDate = LocalDate.parse(startTime, dtf);
            LocalDate endDate = LocalDate.parse(endTime, dtf);
            if (startDate.compareTo(endDate) > 0) {
                return new Return(Return.FAIL, "开始时间不能大于结束时间");
            }
            task.setStartTime(startDate);
            task.setEndTime(endDate);
        } catch (Exception e) {
            return new Return(Return.FAIL, "日期格式不正确");
        }
        return usualTaskService.newUsualTask(task);
    }

    /**
     * 每日报送接口
     *
     * @param userId 用户id
     * @return Return
     */
    @PostMapping("/everydayReport")
    public Return everydayReport(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return usualService.everydayReport(userId);
    }

    /**
     * 获取报送列表接口
     *
     * @param userId 用户id
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getUsualTaskList")
    public Return getUsualTaskList(String userId, Integer page,
                               @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId) || page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return usualTaskService.getUsualTaskList(userId, page, size);
    }

    /**
     * 报送详情接口
     *
     * @param userId 用户id
     * @param usualCode 报送编码
     * @param siteCode 场所编码
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/usualDetails")
    public Return usualDetails(String userId, String usualCode, String siteCode, Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(usualCode) || StringUtils.isEmpty(userId)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page == null || page <= 0) {
            page = 1;
        }
        return usualService.usualDetails(userId, usualCode, siteCode, page, size);
    }

    /**
     * 查询未报送场所列表信息接口
     *
     * @param time 查询时间
     * @return Return
     */
    @PostMapping("/showNotReported")
    public Return showNotReported(String time) {
        if (StringUtils.isEmpty(time)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //解析时间
        LocalDate date;
        try {
            date = LocalDate.parse(time, dtf);
        } catch (Exception e) {
            return new Return(Return.FAIL, "日期格式错误");
        }
        return usualService.showNotReported(date);
    }

    /**
     * 一键报送提醒接口
     *
     * @param siteCodes 未报送的场所编码
     * @return Return
     */
    @PostMapping("/usualReminder")
    public Return usualReminder(String siteCodes) {
        if (StringUtils.isEmpty(siteCodes)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return usualService.usualReminder(siteCodes);
    }

    /**
     * 报送任务详情
     *
     * @param usualCode 报送编码
     * @return Return
     */
    @PostMapping("/usualTaskDetails")
    public Return usualTaskDetails(String usualCode) {
        if (StringUtils.isEmpty(usualCode)) {
            return new Return(Return.FAIL, "usualCode 不能为空");
        }
        return new Return(usualTaskService.usualTaskDetails(usualCode));
    }

    /**
     * 报送任务修改
     *
     * @param usualCode 报送编码
     * @param title 主题
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return Return
     */
    @PostMapping("/update")
    public Return update(String usualCode, String title, String startTime, String endTime) {
        if (StringUtils.isEmpty(usualCode) || StringUtils.isEmpty(title) || "".equals(title.trim())
                || StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (title.trim().length() > 500) {
            return new Return(Return.FAIL, "标题过长");
        }
        return usualTaskService.update(usualCode, title, startTime, endTime);
    }

    /**
     * 删除报送任务
     *
     * @param usualCode 报送编码
     * @return Return
     */
    @PostMapping("/delete")
    public Return delete(String usualCode) {
        if (StringUtils.isEmpty(usualCode)) {
            return new Return(Return.FAIL, "usualCode 不能为空");
        }
        usualTaskService.delete(usualCode);
        return new Return(Return.SUCCESS, "删除成功");
    }
}
